package cn.lau.demo.util;

import cn.hutool.core.util.IdUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author War horse imwarhorse@aliyun.com
 * Date: 2021/1/4
 * Description: 加密工具
 */

public class EncryptUtil {

	private EncryptUtil() {
	}

	private final static String ALG_NAME = "MD5";

	/**
	 * 生成加密后的密码
	 *
	 * @param password 明文密码
	 * @return 带salt 和 password(密文)的Map集合
	 */
	public static Map<String, String> generatePassword(String password) {
		String uuidAsSalt = IdUtil.randomUUID();    // 存入db
		Object saltBytes = ByteSource.Util.bytes(uuidAsSalt);
		SimpleHash simpleHash = new SimpleHash(ALG_NAME, password, saltBytes);

		Map<String, String> result = new HashMap<>();
		result.put("salt", uuidAsSalt);
		result.put("password", simpleHash.toString());
		return result;
	}

}
